﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WpfDPAndAPDemo.DP
{
    /// <summary>
    /// 附加属性
    /// </summary>
    public class TextBoxHelper
    {
        #region title
        public static string GetTitle(DependencyObject obj)
        {
            return (string)obj.GetValue(TitleProperty);
        }

        public static void SetTitle(DependencyObject obj, string value)
        {
            obj.SetValue(TitleProperty, value);
        }

        public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached(
        "Title",
        typeof(string),
        typeof(TextBoxHelper),
        new PropertyMetadata("")
            );

        #endregion


        #region HasText

        public static bool GetHasText(DependencyObject obj)
        {
            return (bool)obj.GetValue(HasTextProperty);
        }

        public static void SetHasText(DependencyObject obj, bool value)
        {
            obj.SetValue(HasTextProperty, value);
        }

        public static readonly DependencyProperty HasTextProperty = DependencyProperty.RegisterAttached(
            "HasText",
            typeof(bool),
            typeof(TextBoxHelper),
            new PropertyMetadata(false, callback));//在这里直接添加回调函数行不通，因为该回调为属性改变的回调，hastext属性未改变时不会触发该回调函数，因此需要一个监控属性来监控回调的产生

        private static void callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is not TextBox box)
                throw new NotSupportedException();
            box.TextChanged += TextChanged;
        }



        ////监控属性
        //public static bool GetMonitorTextChangedPropertyChanged(DependencyObject obj)
        //{
        //    return (bool)obj.GetValue(MonitorTextChangedPropertyChangedProperty);
        //}

        //public static void SetMonitorTextChangedPropertyChanged(DependencyObject obj, bool value)
        //{
        //    obj.SetValue(MonitorTextChangedPropertyChangedProperty, value);
        //}

        //public static readonly DependencyProperty MonitorTextChangedPropertyChangedProperty = DependencyProperty.RegisterAttached(
        //    "MonitorTextChangedPropertyChanged",
        //    typeof(bool),
        //    typeof(TextBoxHelper),
        //    new PropertyMetadata(
        //        false,
        //        MonitorTextChangedPropertyChanged)
        //    );


        //private static void MonitorTextChangedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        //{
        //    if (d is not TextBox box)
        //        throw new NotSupportedException();
        //    if ((bool)e.NewValue)
        //    {
        //        box.TextChanged += TextChanged;
        //        SetHasText(box, !string.IsNullOrEmpty(box.Text));
        //    }
        //    else
        //    {
        //        box.TextChanged -= TextChanged;
        //    }
        //}

        private static void TextChanged(object sender, TextChangedEventArgs e)
        {
            var box = (TextBox)sender;
            SetHasText(box, !string.IsNullOrEmpty(box.Text));
        }

        #endregion

    }
}
